package controllers

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

// 响应格式
type ResponseFormat struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
	Data string `json:"data"`
}

type Controller struct {
}

/**
 * 控制器默认初始化函数
 */
func (c *Controller) Init() {
}

/**
 * 失败响应
 */
func (c *Controller) BadResponse(code int, data string, context *gin.Context) {
	format := ResponseFormat{
		Code: 10000,
		Msg:  badCode[10000],
		Data: data,
	}

	if badMsg, exist := badCode[code]; exist {
		format.Code = code
		format.Msg = badMsg
	}

	context.JSON(200, format)
}

/**
 * 成功响应
 */
func (c *Controller) JsonResponse(data interface{}, context *gin.Context) {
	context.JSON(
		http.StatusOK,
		struct {
			ResponseFormat
			Data interface{} `json:"data"`
		}{
			ResponseFormat: ResponseFormat{
				Code: 0,
				Msg:  "成功",
			},
			Data: data,
		},
	)
}

/**
 * 错误码
 */
var badCode = map[int]string{
	10000: "参数错误",
	10001: "解析POST数据失败",
	10002: "禁止访问",
	10003: "项目不能为空",
	10004: "获取不到 appid",

	10400: "加密参数异常",
	10401: "加密校验失败",
	10501: "IP被禁止，请添加白名单",
	10601: "AES加密解密失败!",
	10602: "获取重定向链接失败!",

	20000: "延时队列入库失败!",
}
